package com.lby.system.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lby.handler.BusinessException;
import com.lby.response.Result;
import com.lby.response.ResultCode;
import com.lby.system.entity.TbUser;
import com.lby.system.service.AliOssService;
import com.lby.system.service.TbUserService;
import com.lby.system.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lby
 * @since 2021-05-05
 */
@RestController
@RequestMapping("/system/tb-user")
// 解决跨域
//@CrossOrigin
@Api(value = "系统用户模块", tags = "系统用户接口")
public class TbUserController {

    @Resource
    private TbUserService tbUserService;

    @Autowired
    private AliOssService aliOssService;

    @GetMapping
    @ApiOperation(value = "用户列表", notes = "查询所有用户信息")
    public Result findUsers() {
        List<TbUser> list = tbUserService.list();
//        int i = 1/0;
        return Result.ok().data("users", list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个用户", notes = "通过id查询单个用户")
    public Result getUserById(@PathVariable("id") Long id) {
        TbUser user = tbUserService.getById(id);
        if(user != null) {
            return Result.ok().data("user", user);
        } else {
            throw new BusinessException(ResultCode.USER_NOT_FOUND_EXCEPTION.getCode(), ResultCode.USER_NOT_FOUND_EXCEPTION.getMessage());
        }
    }

    /*------------------------------------接口已停用----------------------------------------------------------*/
    /**
     * 分页查询用户列表
     * @return
     */
    @GetMapping("/findUserList")
    @ApiOperation(value = "分组查询用户列表", notes = "通过'current'以及'size'进行分组查询用户列表")
    public Result findUserList(@RequestParam(required = true, defaultValue = "1") Integer current,
                               @RequestParam(required = true, defaultValue = "6") Integer size) {
        // 对用户进行分页，泛型中注入的就是用户实类
        Page<TbUser> page = new Page<>(current, size);
        // 单表操作比较方便,多表时需重写
        LambdaQueryWrapper<TbUser> queryWrapper = new LambdaQueryWrapper<>();
        Page<TbUser> userPage = tbUserService.page(page);
        long total = userPage.getTotal();
        List<TbUser> records = userPage.getRecords();
        return Result.ok().data("total", total).data("records", records);

    }
    /*--------------------------------------------------------------------------------------------------------*/


    /**
     * 根据条件分页查询用户列表
     * @return
     *
     * @RequestBody: 主要用来接收前端传递后端的json字符串中的数据
     * 前端只能用POST方式进行提交
     */
    @PostMapping("/findUserPage")
    @ApiOperation(value = "根据条件分组查询用户列表", notes = "通过'current'以及'size'进行分组查询用户列表，userVo 筛选条件")
    public Result findUserPage(@RequestParam(required = true, defaultValue = "1") Integer current,
                               @RequestParam(required = true, defaultValue = "6") Integer size,
                               @RequestBody UserVO userVO) {
        // 对用户进行分页，泛型中注入的就是用户实类
        Page<TbUser> page = new Page<>(current, size);
        QueryWrapper<TbUser> wrapper = getWrapper(userVO);
        IPage<TbUser> userPage = tbUserService.findUserPage(page, wrapper);
        long total = userPage.getTotal();
        List<TbUser> records = userPage.getRecords();
        return Result.ok().data("total", total).data("records", records);

    }

    /**
     * 封装用户查询列表条件
     * @param userVO
     * @return QueryWrapper<TbUser> queryWrapper
     */
    private QueryWrapper<TbUser> getWrapper(UserVO userVO) {
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<TbUser>();
        if(userVO != null) {
            if (!StringUtils.isEmpty(userVO.getDepartmentId())) {
                queryWrapper.eq("department_id", userVO.getDepartmentId());
            }
            if (!StringUtils.isEmpty(userVO.getUsername())) {
                queryWrapper.eq("username", userVO.getUsername());
            }
            if (!StringUtils.isEmpty(userVO.getNickname())) {
                queryWrapper.eq("nickname", userVO.getNickname());
            }
            if (!StringUtils.isEmpty(userVO.getEmail())) {
                queryWrapper.eq("email", userVO.getEmail());
            }
            if (!StringUtils.isEmpty(userVO.getSex())) {
                queryWrapper.eq("sex", userVO.getSex());
            }
        }
            return  queryWrapper;
    }

    @ApiOperation(value = "Oss上传文件", notes = "通过AliOSS上传文件")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        String upload = aliOssService.upload(file);
        return Result.ok().message(upload);
    }







}

